function ModuleDialogAddPurchase(doc, container, width, height, user, date, options, data) {
  this._doc = doc;
  this._container = container;
  this._width = width;
  this._height = height;
  this._user = user;
  this._date = date;
  this._options = options;
  this._data = data;
  
  this._item = (options && options.task) ? options.task : null;
  this._popupBox = new ModulePopupBoxSimple();
  this._popupBox2 = new ModulePopupBox();
  this._photographers = this._data['photographer'].data;
  this._cultures = this._data['culture'].data;
  this._ceremonys = this._data['ceremony'].data;
  this._receptions = this._data['reception'].data;
  this._colors = this._data['color'].data;
  
  MiscUtils.sort(this._cultures, ['name']);
  MiscUtils.sort(this._ceremonys, ['name']);
  MiscUtils.sort(this._receptions, ['name']);
  MiscUtils.sort(this._colors, ['name']);
  MiscUtils.sort(this._photographers, ['name']);
  
  this._models = this._data['pz_model'];
  this._states = this._data['pz_model_state'];
  this._transitions = this._data['pz_model_transition'];
  
  this._item.model_id = (this._item) ? this._item.model_id : 1;
  this._item.state_id = (this._item) ? this._item.state_id : 1;
  this._item.received_date = 0;
  this._item.backuped_date = 0;
  this._item.chosen_date = 0
  this._item.asstretouched_date = 0;
  this._item.checked_date = 0;
  this._item.adminretouched_date = 0;
  this._item.choosedesigner_date = 0
  this._item.retouched_date = 0;
  this._item.sorted_date = 0;
  this._item.prearrange_date = 0;
  this._item.authorarranged_date = 0;
  this._item.authorreceived_date = 0;
  this._item.published_date = 0;
  this._item.confirmpublished_date = 0;
  this._item.deadline = parseInt(this._date, 10) + 86400 * 3;
  
  this._gui = new DialogAddPurchase(this._doc, this._container, this._width, this._height, this._user, this._date, this._options, this._data)._gui;
  this._createElements();
};

ModuleDialogAddPurchase.prototype._createElements = function() {
  var unique = MiscUtils.unique(), _self = this;

  PPublishUtils.clear(this._gui);
  
  this._gui.branch.options[this._gui.branch.options.length] = new Option('-');
  for (var i = 0, il = Branch.array().length; i < il; i++) {
    var branch = Branch.array()[i];
    option = new Option(branch);
    this._gui.branch.options[this._gui.branch.options.length] = option;
    if (option.text == this._item.branch) {
      this._gui.branch.selectedIndex = this._gui.branch.options.length - 1;
    }
  }
  this._gui.branch.onchange = function() { _self._item.branch = (this.options[this.selectedIndex].text != '-') ? this.options[this.selectedIndex].text : '';
                                           _self._createElements();
                                         };
  this._gui.culture1.options[this._gui.culture1.options.length] = new Option('-');
  for (var i = 0, il = this._cultures.length; i < il; i++) {
    var culture = this._cultures[i];
    option = new Option(culture.name);
    this._gui.culture1.options[this._gui.culture1.options.length] = option;
    if (option.text == this._item.culture1) {
      this._gui.culture1.selectedIndex = this._gui.culture1.options.length - 1;
    }
  }
  this._gui.culture1.onchange = function() { _self._item.culture1 = (this.options[this.selectedIndex].text != '-') ? this.options[this.selectedIndex].text : ''; };
  
  this._gui.addculture1.onclick = function() { var object = pz_culture.instance();
                                               var div = document.createElement('div');
                                               var pos = DOMUtils.findPos(this);
                                               var func1 = function() { _self._popupBox2._close();
                                                                        _self._retrieveCultures.call(_self);
                                                                      };
                                               var func = function() { _self._popupBox2._close();
                                                                       _self._item.culture1 = object.name;
                                                                       new RequestUtils()._write('pz_culture', [object], [], function() { _self._retrieveCultures.call(_self); }, { pos: pos });
                                                                     };
                                               MiscUtils.dialog(_self._popupBox2, pos, div, func1, null, { t: 'Add Culture', width: 750, height: 50, func: func1});
                                               new ModuleDialogObject(document, div, 460, 100, _self._user, _self._date, { object: object, name: 'pz_culture', title: 'Cultures', textTitle: 'Culture', callbackFunc: func });
                                             };
  
  this._gui.culture2.options[this._gui.culture2.options.length] = new Option('-');
  for (var i = 0, il = this._cultures.length; i < il; i++) {
    var culture = this._cultures[i];
    option = new Option(culture.name);
    this._gui.culture2.options[this._gui.culture2.options.length] = option;
    if (option.text == this._item.culture2) {
      this._gui.culture2.selectedIndex = this._gui.culture2.options.length - 1;
    }
  }
  this._gui.culture2.onchange = function() { _self._item.culture2 = (this.options[this.selectedIndex].text != '-') ? this.options[this.selectedIndex].text : ''; };
  
  this._gui.addculture2.onclick = function() { var object = pz_culture.instance();
                                                var div = document.createElement('div');
                                                var pos = DOMUtils.findPos(this);
                                                var func1 = function() { _self._popupBox2._close();
                                                                         _self._retrieveCultures.call(_self);
                                                                       };
                                                var func = function() { _self._popupBox2._close();
                                                                        _self._item.culture2 = object.name;
                                                                        new RequestUtils()._write('pz_culture', [object], [], function() { _self._retrieveCultures.call(_self); }, { pos: pos });
                                                                      };
                                                MiscUtils.dialog(_self._popupBox2, pos, div, func1, null, { t: 'Add Culture', width: 750, height: 50, func: func1});
                                                new ModuleDialogObject(document, div, 460, 100, _self._user, _self._date, { object: object, name: 'pz_culture', title: 'Cultures', textTitle: 'Culture', callbackFunc: func });
                                              };
  
  this._gui.ceremony.options[this._gui.ceremony.options.length] = new Option('-');
  for (var i = 0, il = this._ceremonys.length; i < il; i++) {
    var ceremony = this._ceremonys[i];
    option = new Option(ceremony.name);
    this._gui.ceremony.options[this._gui.ceremony.options.length] = option;
    if (option.text == this._item.ceremony) {
      this._gui.ceremony.selectedIndex = this._gui.ceremony.options.length - 1;
    }
  }
  this._gui.ceremony.onchange = function() { _self._item.ceremony = (this.options[this.selectedIndex].text != '-') ? this.options[this.selectedIndex].text : ''; };
  
  this._gui.addceremony.onclick = function() { var object = pz_ceremony.instance();
                                                var div = document.createElement('div');
                                                var pos = DOMUtils.findPos(this);
                                                var func1 = function() { _self._popupBox2._close();
                                                                         _self._retrieveCeremonys.call(_self);
                                                                       };
                                                var func = function() { _self._popupBox2._close();
                                                                        _self._item.ceremony = object.name;
                                                                        new RequestUtils()._write('pz_ceremony', [object], [], function() { _self._retrieveCeremonys.call(_self); }, { pos: pos });
                                                                      };
                                                MiscUtils.dialog(_self._popupBox2, pos, div, func1, null, { t: 'Add Ceremony', width: 750, height: 50, func: func1});
                                                new ModuleDialogObject(document, div, 460, 100, _self._user, _self._date, { object: object, name: 'pz_ceremony', title: 'Ceremonys', textTitle: 'Ceremony', callbackFunc: func });
                                              };

  this._gui.reception.options[this._gui.reception.options.length] = new Option('-');
  for (var i = 0, il = this._receptions.length; i < il; i++) {
    var reception = this._receptions[i];
    option = new Option(reception.name);
    this._gui.reception.options[this._gui.reception.options.length] = option;
    if (option.text == this._item.reception) {
      this._gui.reception.selectedIndex = this._gui.reception.options.length - 1;
    }
  }
  this._gui.reception.onchange = function() { _self._item.reception = (this.options[this.selectedIndex].text != '-') ? this.options[this.selectedIndex].text : ''; };
  
  this._gui.addreception.onclick = function() { var object = pz_reception.instance();
                                                var div = document.createElement('div');
                                                var pos = DOMUtils.findPos(this);
                                                var func1 = function() { _self._popupBox2._close();
                                                                         _self._retrieveReceptions.call(_self);
                                                                       };
                                                var func = function() { _self._popupBox2._close();
                                                                        _self._item.reception = object.name;
                                                                        new RequestUtils()._write('pz_reception', [object], [], function() { _self._retrieveReceptions.call(_self); }, { pos: pos });
                                                                      };
                                                MiscUtils.dialog(_self._popupBox2, pos, div, func1, null, { t: 'Add Reception', width: 750, height: 50, func: func1});
                                                new ModuleDialogObject(document, div, 460, 100, _self._user, _self._date, { object: object, name: 'pz_reception', title: 'Receptions', textTitle: 'Reception', callbackFunc: func });
                                              };
  
  this._gui.weddingcolor.options[this._gui.weddingcolor.options.length] = new Option('-');
  for (var i = 0, il = this._colors.length; i < il; i++) {
    var color = this._colors[i];
    option = new Option(color.name);
    this._gui.weddingcolor.options[this._gui.weddingcolor.options.length] = option;
    if (option.text == this._item.wedding_color) {
      this._gui.weddingcolor.selectedIndex = this._gui.weddingcolor.options.length - 1;
    }
  }
  this._gui.weddingcolor.onchange = function() { _self._item.wedding_color = (this.options[this.selectedIndex].text != '-') ? this.options[this.selectedIndex].text : ''; };
  
  this._gui.addcolor.onclick = function() { var object = pz_color.instance();
                                            var div = document.createElement('div');
                                            var pos = DOMUtils.findPos(this);
                                            var func1 = function() { _self._popupBox2._close();
                                                                     _self._retrieveColors.call(_self);
                                                                   };
                                            var func = function() { _self._popupBox2._close();
                                                                    _self._item.wedding_color = object.name;
                                                                    new RequestUtils()._write('pz_color', [object], [], function() { _self._retrieveColors.call(_self); }, { pos: pos });
                                                                  };
                                            MiscUtils.dialog(_self._popupBox2, pos, div, func1, null, { t: 'Add Color', width: 750, height: 50, func: func1});
                                            new ModuleDialogObject(document, div, 460, 100, _self._user, _self._date, { object: object, name: 'pz_color', title: 'Colors', textTitle: 'Color', callbackFunc: func });
                                          };
  
  this._gui.cusNo.value = this._item.customer_id;
  this._gui.cusNo.onchange = function() { _self._item.customer_id = this.value };
  
  this._gui.bName.value = this._item.bride_name;
  this._gui.bName.onchange = function() { _self._item.bride_name = this.value };
  
  this._gui.gName.value = this._item.groom_name;
  this._gui.gName.onchange = function() { _self._item.groom_name = this.value };
  
  this._gui.is_wedding.checked = (this._item.is_wedding == 1) ? true : false;
  this._gui.is_wedding.onchange = function() { if (this.checked) {
                                                 _self._item.is_wedding = 1;
                                               } else {
                                                 _self._item.is_wedding = 0;
                                               }
                                             };
  
  this._gui.weddingdate.value = (this._item.wedding_date != 0) ? SimpleDate.format(SimpleDate.fromStamp(this._item.wedding_date)) : '';
  this._gui.weddingdate.onclick = function() { var context = this;
                                               _self._changeDate.call(_self, this, SimpleDate.fromStamp(_self._item.wedding_date), function(sd) { _self._item.wedding_date = SimpleDate.toStamp(sd); context.focus(); }, false);
                                               return false;
                                             };
  
  this._gui.quantity.value = this._item.pi_quantity;
  this._gui.quantity.onchange = function() { _self._item.pi_quantity = this.value };
      
  this._gui.photographer.options[this._gui.photographer.options.length] = new Option('-');
  for (var i = 0, il = this._photographers.length; i < il; i++) {
    var u = this._photographers[i];
//    if (u.branch != this._item.branch) {
//      continue;
//    }
    if (this._item.branch == Constant.BRANCH_AKL) {
      if (u.is_akl == 0) {
        continue;
      }
    } else if (this._item.branch == Constant.BRANCH_SYD) {
      if (u.is_syd == 0) {
        continue;
      }
    } else if (this._item.branch == Constant.BRANCH_MEL) {
      if (u.is_mel == 0) {
        continue;
      }
    } else if (this._item.branch == Constant.BRANCH_XTRAO) {
      if (u.is_xtrao == 0) {
        continue;
      }
    } else if (this._item.branch == Constant.BRANCH_NYC) {
      if (u.is_nyc == 0) {
        continue;
      }
    } else if (this._item.branch == Constant.BRANCH_BNE) {
      if (u.is_bne == 0) {
        continue;
      }
    } else {
      continue;
    }
    var str = PPublishUtils.branchShow(u);
    option = new Option(u.name + '(' + str + ')');
    option._u = u;
    this._gui.photographer.options[this._gui.photographer.options.length] = option;
    if (option._u.id == this._item.photographer_id) {
      this._gui.photographer.selectedIndex = this._gui.photographer.options.length - 1;
    }
  }
  
  this._gui.photographer.onchange = function() { _self._item.photographer_id = (this.options[this.selectedIndex]._u) ? this.options[this.selectedIndex]._u.id : 0; };
  
  this._gui.photographer2.options[this._gui.photographer2.options.length] = new Option('-');
  for (var i = 0, il = this._photographers.length; i < il; i++) {
    var u = this._photographers[i];
//    if (u.branch != this._item.branch) {
//      continue;
//    }
    if (this._item.branch == Constant.BRANCH_AKL) {
      if (u.is_akl == 0) {
        continue;
      }
    } else if (this._item.branch == Constant.BRANCH_SYD) {
      if (u.is_syd == 0) {
        continue;
      }
    } else if (this._item.branch == Constant.BRANCH_MEL) {
      if (u.is_mel == 0) {
        continue;
      }
    } else if (this._item.branch == Constant.BRANCH_XTRAO) {
      if (u.is_xtrao == 0) {
        continue;
      }
    } else if (this._item.branch == Constant.BRANCH_NYC) {
      if (u.is_nyc == 0) {
        continue;
      }
    } else if (this._item.branch == Constant.BRANCH_BNE) {
      if (u.is_bne == 0) {
        continue;
      }
    } else {
      continue;
    }
    var str = PPublishUtils.branchShow(u);
    option = new Option(u.name + '(' + str + ')');
    option._u = u;
    this._gui.photographer2.options[this._gui.photographer2.options.length] = option;
    if (option._u.id == this._item.photographer2_id) {
      this._gui.photographer2.selectedIndex = this._gui.photographer2.options.length - 1;
    }
  }
  
  this._gui.photographer2.onchange = function() { _self._item.photographer2_id = (this.options[this.selectedIndex]._u) ? this.options[this.selectedIndex]._u.id : 0; };
};

ModuleDialogAddPurchase.prototype._changeDate = function(label, currentDate, onChangeFunc, showTime) {
  var _self = this;
  this._wContainer = document.createElement('div');
  this._wContainer.style.left = DOMUtils.findPos(label)[0] + 'px';
  this._wContainer.style.top = DOMUtils.findPos(label)[1] + 'px';
  this._popupBox._open(this._wContainer, { pos: DOMUtils.findPos(label) });
  
  var ds = new DateSelect(currentDate, null, { container: this._wContainer, showTime: showTime });
  ds._selectFunc = function(sd) { _self._selectDate.call(_self, sd, label, onChangeFunc, showTime); };
};

ModuleDialogAddPurchase.prototype._selectDate = function(sd, label, onChangeFunc, showTime) {
  
  onChangeFunc(sd);
  this._showDate(sd, label, showTime);
  this._popupBox._close();
};

ModuleDialogAddPurchase.prototype._showDate = function(sd, label, showTime) {
  if (sd) { 
    label.value = SimpleDate.format(sd, showTime);
  } else {
    label.value = '';
  }
};

ModuleDialogAddPurchase.prototype._retrieveCultures = function() {
  var _self = this;
  var args = '';
  new RequestUtils()._mysql('getCultures', args, function(result, params) { _self._cultures = result;
                                                                            _self._createElements();
                                                                          },  null);
};

ModuleDialogAddPurchase.prototype._retrieveCeremonys = function() {
  var _self = this;
  var args = '';
  new RequestUtils()._mysql('getCeremonys', args, function(result, params) { _self._ceremonys = result;
                                                                             _self._createElements();
                                                                           },  null);
};

ModuleDialogAddPurchase.prototype._retrieveReceptions = function() {
  var _self = this;
  var args = '';
  new RequestUtils()._mysql('getReceptions', args, function(result, params) { _self._receptions = result;
                                                                              _self._createElements();
                                                                            },  null);
};

ModuleDialogAddPurchase.prototype._retrieveColors = function() {
  var _self = this;
  var args = '';
  new RequestUtils()._mysql('getColors', args, function(result, params) { _self._colors = result;
                                                                          _self._createElements();
                                                                        },  null);
};